package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.HealthBasin;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 养生盆数据库操作
 */

public class HealthBasinDao {
    private Context mContext;
    private Dao<HealthBasin, Integer> healthBasinDaoOpe;
    private DatabaseHelper helper;

    public HealthBasinDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            healthBasinDaoOpe = helper.getDao(HealthBasin.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(HealthBasin healthBasin) {
        try {
            healthBasin.setChecked(false);
            healthBasinDaoOpe.create(healthBasin);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateHealthBasin(HealthBasin healthBasin) {
        try {
            healthBasin.setChecked(false);
            return healthBasinDaoOpe.update(healthBasin);
            //    LogHelper.i("updateLightHealthBasinnumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public HealthBasin get(int id) {
        try {
            return healthBasinDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public HealthBasin getByUuid(String uuid) {
        List<HealthBasin> healthBasinList = null;
        try {
            healthBasinList = healthBasinDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (healthBasinList != null && healthBasinList.size() > 0) {
                return healthBasinList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<HealthBasin> getAllHealthBasin() {
        List<HealthBasin> healthBasinList = new ArrayList<>();
        try {
            healthBasinList = healthBasinDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return healthBasinList;
    }

    public HealthBasin getById(String groupId,int category) {
        List<HealthBasin> healthBasinList = null;
        try {
            healthBasinList = healthBasinDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (healthBasinList != null && healthBasinList.size() > 0) {
                return healthBasinList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<HealthBasin> getFamilyHealthBasins(String familyId,int category) {
        List<HealthBasin> healthBasinList = new ArrayList<>();
        try {
            healthBasinList = healthBasinDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return healthBasinList;
    }

    public int delete(HealthBasin healthBasin) {
        try {
            return healthBasinDaoOpe.delete(healthBasin);
            //    LogHelper.i("deleLightHealthBasinnumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = healthBasinDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<HealthBasin> healthBasinList) {
        try {
            int number = healthBasinDaoOpe.delete(healthBasinList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyHealthBasins(String familyId) {
        try {
            List<HealthBasin> healthBasinList = getFamilyHealthBasins(familyId,9);
            int number = healthBasinDaoOpe.delete(healthBasinList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public HealthBasin getHealthBasinByName(String familyId, String name,int category) {
        try {
            List<HealthBasin> healthBasinList = healthBasinDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (healthBasinList != null && healthBasinList.size() > 0)
                return healthBasinList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
